from .EasyToolPlus import ApkDecompiler, decompile_apk2,decompile_apk1
from .apkinfoform import APKInfoForm
from joblib import load
import numpy as np


def get_apkinfo(apk_name):
    input_path='/'.join(['apkfilter',apk_name]) #
    output_path='/'.join(['filter',apk_name])
    apkinfo = ApkDecompiler(input_path, output_path)

# apk_filename,activities,package,permissions,app_name,version_name,version_code,hash_algo,serial_number,
# contents_der,issuer,md5,sha1,sha256,validity_period,signature_algo,intent,icon_url,apk_size,folder_size
    apk_filename=apk_name
    activities=apkinfo.get_activities()
    package=apkinfo.get_package()
    permissions=apkinfo.get_permissions()
    app_name=apkinfo.get_app_name()
    version_name=apkinfo.get_androidversion_name()
    version_code=apkinfo.get_androidversion_code()
    hash_algo=apkinfo.get_hash_algo()
    serial_number=apkinfo.get_serial_number()
    contents_der=apkinfo.get_contents_der()
    issuer=apkinfo.get_issuer()
    md5=apkinfo.get_md5()
    sha1=apkinfo.get_sha1()
    sha256=apkinfo.get_sha256()
    validity_period=apkinfo.get_validity_period()
    signature_algo=apkinfo.get_signature_algo()
    intent=apkinfo.get_intent()
    icon_url=apkinfo.get_app_icon()
    apk_size=apkinfo.get_apk_size()
    try:
        folder_size=apkinfo.get_folder_size()
    except AttributeError:
        folder_size=apk_size*4.5
    apkinfoform=APKInfoForm(apk_filename,activities,package,permissions,app_name,version_name,version_code,
                            hash_algo,serial_number,contents_der,issuer,md5,sha1,sha256,validity_period
                            ,signature_algo,intent,icon_url,apk_size,folder_size)
    return np.array(apkinfoform.get_info()).reshape(1, 23),apkinfoform

def get_ret(x):
    model_file_path = r"D:\CODE_EDIT\pythonproject\djangoProject\model\utils\v1.joblib"
    loaded_rf = load(model_file_path)
    predictions = loaded_rf.predict(x)
    return predictions

# # t=get_apkinfo('110_68f99f93342500221ff919f0d9577397.apk')
# t1,t2=get_apkinfo('106_0a855b5a86f082c4efa7f89640f7ba30.apk')
# r=get_ret(t1)
# print(r[0])
# print(type(t),':',t)

def unzip_apk(file_name):
    input_path='/'.join(['apkfilter',file_name]) #
    output_path='/'.join(['filter',file_name])
    try:
        decompile_apk2(input_path,output_path)
    except Exception as e:
        decompile_apk1(input_path,output_path)
    return output_path